#include "Plane.h"

Plane::Plane (uint x, uint y,
              Vertex* first, Vertex* second, Vertex* third) : _X (x), _Y (y)
{
  set_vertex (first, second, third);
}


void Plane::calc_coef()
{
  calc_normal ();
  float D = _Normal[0] * (*_Vertex[0])[0]
          + _Normal[1] * (*_Vertex[0])[1] + _Normal[2] * (*_Vertex[0])[2];
  _AdC = _Normal[0] / _Normal[2];
  _BdC = _Normal[1] / _Normal[2];
  _DdC = D / _Normal[2];
}


void Plane::calc_normal()
{
  _3D_Vector first_vec (_Vertex[0]->position (), _Vertex[2]->position ());
  _3D_Vector second_vec (_Vertex[0]->position (), _Vertex[1]->position ());
  _Normal = first_vec.cross (second_vec);
  _Normal.unitary ();
  //for (int i = 0; i < 3; ++i) _Vertex[i]->set_normal (_X, _Y, _Normal);
}


Plane& Plane::operator= (const Plane& right)
{
  _Vertex[0] = right._Vertex[0];
  _Vertex[1] = right._Vertex[1];
  _Vertex[2] = right._Vertex[2];
  _AdC = right._AdC; _BdC = right._BdC; _DdC = right._DdC;
  _Normal = right._Normal;
  _X = right._X; _Y = right._Y;
  return *this;
}


void Plane::set_vertex (Vertex* first, Vertex* second, Vertex* third)
{
  _Vertex[0] = first;
  _Vertex[1] = second;
  _Vertex[2] = third;
  calc_coef ();
}
